import * as types from './mutation-types'
import {playMode} from '@/common/js/config'
import {shuffle} from '@/common/js/util'

function findIndex(list, song) {
  return list.findIndex((item) => {
    return item.id === song.id
  })
}
export function selectPlay({commit, state}, {list, index}) {
  // 顺序列表
  commit(types.SET_SEQUENCE_LIST, list)
  // 解决 模式切换到随机之后 点击播放选择的歌曲 播放的去不是自己选择的 因为 list[index] 变化了
  if (state.mode === playMode.random) {
    let randomList = shuffle(list)
    commit(types.SET_PLAYLSIT, randomList)
    index = findIndex(randomList, list[index])
  } else {
    // 设置播放列表
    commit(types.SET_PLAYLSIT, list)
  }
  // 当前播放歌曲的索引值
  commit(types.SET_CURRENT_INDEX, index)
  // 全屏播放
  commit(types.SET_FULL_SCREEN, true)
  // 设置播放状态
  commit(types.SET_PLAYING_STATE, true)
}

export function randomPlay({commit}, {list}) {
  commit(types.SET_PLAY_MODE, playMode.random)
  commit(types.SET_SEQUENCE_LIST, list)
  commit(types.SET_PLAYLSIT, shuffle(list))
  commit(types.SET_FULL_SCREEN, true)
  commit(types.SET_CURRENT_INDEX, 0)
  commit(types.SET_PLAYING_STATE, true)
}